package ${basePackage}.controller;

import ${basePackage}.common.api.CommonPage;
import ${basePackage}.common.api.CustomResult;
import ${basePackage}.model.${modelNameUpperCamel};
import ${basePackage}.service.${modelNameUpperCamel}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by CodeGenerator on 2024/02/07.
 */
@Controller
@Api(tags = "${modelNameUpperCamel}Controller")
@Tag(name = "${modelNameUpperCamel}Controller", description = "${baseRequestMapping}管理")
@RequestMapping("/<#noparse>${server.api-path}</#noparse>/${baseRequestMapping}")
public class ${modelNameUpperCamel}Controller {

    @Autowired
    private ${modelNameUpperCamel}Service ${modelNameLowerCamel}Service;

    @ApiOperation("新增")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> create(@Validated @RequestBody ${modelNameUpperCamel} ${modelNameLowerCamel}) {
        int count = ${modelNameLowerCamel}Service.create(${modelNameLowerCamel});
        if (count > 0) {
            return new CustomResult().success(count).getResult();
        } else {
            return new CustomResult().failed().getResult();
        }
    }

    @ApiOperation("修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> update(@Validated @RequestBody ${modelNameUpperCamel} ${modelNameLowerCamel}) {
        Long id = ${modelNameLowerCamel}.getId();
        if (id == null) {
            return new CustomResult().failed("id不能为空").getResult();
        }
        int count = ${modelNameLowerCamel}Service.update(id, ${modelNameLowerCamel});
        if (count > 0) {
            return new CustomResult().success(count).getResult();
        } else {
            return new CustomResult().failed().getResult();
        }
    }

    @ApiOperation("根据ID获取详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getItem(@PathVariable Long id) {
        ${modelNameUpperCamel} ${modelNameLowerCamel} = ${modelNameLowerCamel}Service.getItem(id);
        return new CustomResult().success(${modelNameLowerCamel}).getResult();
    }

    @ApiOperation("根据ID删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delete(@RequestBody ${modelNameUpperCamel} ${modelNameLowerCamel}) {
        Long id = ${modelNameLowerCamel}.getId();
        if (id == null) {
            return new CustomResult().failed("id不能为空").getResult();
        }
        int count = ${modelNameLowerCamel}Service.delete(id);
        if (count > 0) {
            return new CustomResult().success(count).getResult();
        } else {
            return new CustomResult().failed().getResult();
        }
    }

    @ApiOperation("根据条件分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(@RequestBody(required = false) ${modelNameUpperCamel} ${modelNameLowerCamel},
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<${modelNameUpperCamel}> resultList = ${modelNameLowerCamel}Service.list(${modelNameLowerCamel}, pageSize, pageNum);
        return new CustomResult().success(resultList).setProperty("count", CommonPage.restPage(resultList).getTotal())
                .getResult();
    }

    @ApiOperation("查询所有数据（默认限制1000条）")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> listAll(
            @RequestParam(value = "pageSize", defaultValue = "1000") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        ${modelNameUpperCamel} ${modelNameLowerCamel} = new ${modelNameUpperCamel}();
        List<${modelNameUpperCamel}> resultList = ${modelNameLowerCamel}Service.list(${modelNameLowerCamel}, pageSize, pageNum);
        return new CustomResult().success(resultList).setProperty("count", CommonPage.restPage(resultList).getTotal())
                .getResult();
    }
}
